<%@page  import="merlin.beans.*,merlin.dao.*,merlin.managers.*,merlin.servlets.*,merlin.utils.*" errorPage="mr_errhandler.jsp" %>
<%
	// sanity check
	if(session.getAttribute("username") == null){
		response.sendRedirect("login.jsp");
		return;
	}
%>

<HTML>
<HEAD>
	<meta HTTP-EQUIV="Cache-Control"  CONTENT="no-cache" />
	<meta HTTP-EQUIV="Expires"        CONTENT="1 Jan 1980" />
	<meta HTTP-EQUIV="MaxAge"         CONTENT="0" />
	<meta HTTP-EQUIV="Pragma-Control" CONTENT="no-cache" />

<script>
	var arrCompanyRecords = new Array();
	var arrDirectorRecords = new Array();
	var address , businessDesription, shareholders, financialDetails, parentSubsidiaryDetails;
<%
	DbResultSet result = (DbResultSet)request.getAttribute("result");
	if(result != null && result.size() > 0){
		out.println(result.size());
		for(int i=0;i<result.size();i++){
			LookupDetailsBean bean = (LookupDetailsBean)result.get(i);
			out.println("arrCompanyRecords[" + i + "] = new Array(\"" + bean.getCompanyNumber() + "\",\"" + bean.getCompanyName() + "\")");
		}
		// Retrieve the first companies address.
		LookupDetailsBean bean = (LookupDetailsBean)result.get(0);
    	StringBuffer address = new StringBuffer();
		String s = bean.getAddressLine1();
		if(s != null && s.length() > 0)address.append(s).append("<BR>");
		s = bean.getAddressLine2();
		if(s != null && s.length() > 0)address.append(s).append("<BR>");
		s = bean.getTown();
		if(s != null && s.length() > 0)address.append(s).append("<BR>");
		s = bean.getCounty();
		if(s != null && s.length() > 0)address.append(s).append("<BR>");
		s = bean.getPostcode();
		if(s != null && s.length() > 0)address.append(s);
		if(address.length() == 0)address.append("[No address found]");
		out.println("address = \"" + address + "\"");
		s = bean.getBusinessDescription();
		if(s == null || s.length() == 0){
			s ="[No description found]";
		}
		out.println("businessDesription = \"" + s + "\"");

		// info about share holders
		DbResultSet dbRes = (DbResultSet)request.getAttribute("shareholderDetails");
		if(dbRes != null && dbRes.size() > 0){
			// create the shareholder string
			StringBuffer sbShare = new StringBuffer("<table border=1 cellspacing = 0 width='100%' style='FONT: 11px arial, verdana, sans-serif;'><thead><tr style='background-color:#A5CEFF;'><th >Shareholder</th><th style=''>Shareholdings</th></tr></thead><tbody class='overflow:auto'>");
			for(int i=0;i< dbRes.size();i++){
    			ShareholderDetailsBean sdbBean = (ShareholderDetailsBean)dbRes.get(i);
    			sbShare.append("<tr><td align='center' style=''>").append((sdbBean.getShareholder()!=null?sdbBean.getShareholder():"&nbsp;")).append("</td><td style='text-align:right'>").append((sdbBean.getShareholding()!=null?sdbBean.getShareholding():"&nbsp;")).append("</td></tr>");

			}
			sbShare.append("</tbody></table>");
			out.println("shareholders = \"" + sbShare.toString() + "\"");
		}else{
			out.println("shareholders = \"[No shareholders found]\"");
		}

		// Info about the directors.
		dbRes = (DbResultSet)request.getAttribute("directorDetails");
		if(dbRes != null && dbRes.size() > 0){
			for(int i=0;i< dbRes.size();i++){
				DirectorDetailsBean ddBean = (DirectorDetailsBean)dbRes.get(i);
		    	StringBuffer directorVal = new StringBuffer();
				String sDir = ddBean.getFirstname();
				if(sDir != null)
					directorVal.append(sDir).append('|');
				else
                    directorVal.append('|');
				sDir = ddBean.getSurname();
				if(s != null)
					directorVal.append(sDir).append('|');
				else
                    directorVal.append('|');
				sDir = ddBean.getDateOfBirth();
				if(sDir != null)
					directorVal.append(sDir);
				out.println("arrDirectorRecords[" + i + "] = new Array(\"" + directorVal + "\",\"" + ddBean.getDirector() + "\")");
			}
		}
		//Info about the parent companies.
		dbRes = (DbResultSet)request.getAttribute("parentSubsidiaryDetails");
		if(dbRes != null && dbRes.size() > 0){
			// create the parent string
			StringBuffer sbSubsi = new StringBuffer("<table border=1 cellspacing = 0 width='100%' style='FONT: 11px arial, verdana, sans-serif;'><thead><tr style='background-color:#A5CEFF;'><th>Company</th><th>Turnover</th></tr></thead><tbody class='overflow:auto'>");
			for(int i=0;i< dbRes.size();i++){
    			ParentSubsidiaryDetailsBean psBean = (ParentSubsidiaryDetailsBean)dbRes.get(i);
    			sbSubsi.append("<tr>");
    			if(psBean.getSubsidiaryRegistrationNumber().charAt(0) == '#'){
	    			// these are foreign companies with no details in the DB - grey them out, with no events attached.
    				sbSubsi.append("<td align='left'><span style='color:grey' >").append(psBean.getCompanyName()).append("</td>");
    			}else{
    				sbSubsi.append("<td align='left'><a style='text-decoration:none;color:black' target = 'frameSearch' href='merlin?mode=searchCompanyByParent&hidSearch=").append(psBean.getSubsidiaryRegistrationNumber()).append("'>").append(psBean.getCompanyName()).append("</a></td>");
    			}
				//sbSubsi.append("<td align='left'><a style='text-decoration:none;color:black' href='' onclick='searchCompanyByCompanyNumber(\"").append(psBean.getSubsidiaryRegistrationNumber()).append("\");return false;'>").append(psBean.getCompanyName()).append("</a></td>");
				//sbSubsi.append("<td align='left'><a style='text-decoration:none;color:black' href='' onclick='searchCompanyByCompanyNumber(this.value);return false;' value ='").append(psBean.getSubsidiaryRegistrationNumber()).append("'>").append(psBean.getCompanyName()).append("</a></td>");
				sbSubsi.append("<td style='text-align:right'>").append((psBean.getTurnover()!=null?psBean.getTurnover():"&nbsp;")).append("</td>");
				sbSubsi.append("</tr>");

			}
			sbSubsi.append("</tbody></table>");
			out.println("parentSubsidiaryDetails = \"" + sbSubsi.toString() + "\"");
		}else{
			out.println("parentSubsidiaryDetails = \"[No details found]\"");
		}

		// Info on the financial details.
		dbRes = (DbResultSet)request.getAttribute("financialDetails");
		if(dbRes != null && dbRes.size() > 0){
			// create the financial string
			StringBuffer sbFin = new StringBuffer("<table border=1 cellspacing = 0 width='100%' style='FONT: 11px arial, verdana, sans-serif;'><thead><tr style='background-color:#A5CEFF;'><th>&nbsp;</th><th>Year 1</th><th>Year 2</th><th>Year 3</th><th>Year 4</th></tr></thead><tbody class='overflow:auto'>");
			for(int i=0;i< dbRes.size();i++){
    			FinancialDetailsBean fdBean = (FinancialDetailsBean)dbRes.get(i);
    			sbFin.append("<tr>");
				sbFin.append("<td style='text-align:left'>").append(fdBean.getDescription()).append("</td>");
				sbFin.append("<td style='text-align:right'>").append((fdBean.getYear1()!=null?fdBean.getYear1():"&nbsp;")).append("</td>");
				sbFin.append("<td style='text-align:right'>").append((fdBean.getYear2()!=null?fdBean.getYear2():"&nbsp;")).append("</td>");
				sbFin.append("<td style='text-align:right'>").append((fdBean.getYear3()!=null?fdBean.getYear3():"&nbsp;")).append("</td>");
				sbFin.append("<td style='text-align:right'>").append((fdBean.getYear4()!=null?fdBean.getYear4():"&nbsp;")).append("</td>");
				sbFin.append("</tr>");

			}
			sbFin.append("</tbody></table>");
			out.println("financialDetails = \"" + sbFin.toString() + "\"");
		}else{
			out.println("financialDetails = \"[No records found]\"");
		}
	}
%>
</script>
<script>
	function populateDetails(){

		parent.frameMain.disableEnable(false);

		if(arrCompanyRecords.length > 0){
			// display feedback message
			if(arrCompanyRecords.length ==1){
				//parent.frameMessage.displayMessage("1 record found");
				parent.frameMain.displayMessage("1 record found");
			}else{
				if(arrCompanyRecords.length < 300){
					//parent.frameMessage.displayMessage(arrCompanyRecords.length + " records found");
					parent.frameMain.displayMessage(arrCompanyRecords.length + " records found");
				}else{
					//parent.frameMessage.displayMessage("300+ records found.Search aborted.");
					parent.frameMain.displayMessage("300+ records found.Search aborted.");
				}
			}

			lstCompanies = parent.frameMain.document.frmSearch.lstCompanies;
			// Remove all the previous entries in the lstCompanies select box
			for(i=lstCompanies.length-1;i>=0;i--){
				lstCompanies.remove(i);
			}
			// Now add the new entries
			for(i=0;i< arrCompanyRecords.length;i++){
				var aOption = document.createElement("OPTION");
		    	aOption.value = arrCompanyRecords[i][0];
				aOption.text  = arrCompanyRecords[i][1];
				len = lstCompanies.length;
				lstCompanies.options[len]= aOption;
			}
			// Add the address , shareholders
			var divAddress , divShareholders ,divBusinessDescription,divSummaryFinancial;
			if(document.all){
				// IE 4+
				divAddress 				= parent.frameMain.document.all("divAddress");
				divShareholders 		= parent.frameMain.document.all("divShareholders");
				divBusinessDescription 	= parent.frameMain.document.all("divBusinessDescription");
				divSummaryFinancial 	= parent.frameMain.document.all("divSummaryFinancial");
				divSubsidiary			= parent.frameMain.document.all("divSubsidiary");
			}else if(document.getElementById){
				// Mozilla/NS 6+/Firefox
				divAddress 				= parent.frameMain.document.getElementById("divAddress");
				divShareholders 		= parent.frameMain.document.getElementById("divShareholders");
				divBusinessDescription 	= parent.frameMain.document.getElementById("divBusinessDescription");
				divSummaryFinancial		= parent.frameMain.document.getElementById("divSummaryFinancial");
				divSubsidiary			= parent.frameMain.document.getElementById("divSubsidiary");
			}else if(document.layers){
				divAddress 				= parent.frameMain.document.layers["divAddress"];
				divShareholders 		= parent.frameMain.document.layers["divShareholders"];
				divBusinessDescription 	= parent.frameMain.document.layers["divBusinessDescription"];
				divSummaryFinancial		= parent.frameMain.document.layers["divSummaryFinancial"];
				divSubsidiary			= parent.frameMain.document.getElementById("divSubsidiary");
			}
			divAddress.innerHTML 				= address;
			divShareholders.innerHTML   		= shareholders;
			divBusinessDescription.innerHTML 	= businessDesription;
            divSummaryFinancial.innerHTML		= financialDetails;
            divSubsidiary.innerHTML				= parentSubsidiaryDetails;
			// Add the directors.
			lstDirectors = parent.frameMain.document.frmSearch.lstDirectors;
			// Remove all the previous entries
			for(i=lstDirectors.length-1;i>=0;i--){
				lstDirectors.remove(i);
			}
			// Now add the new entries
			if(arrDirectorRecords.length > 0 ){
				for(i=0;i< arrDirectorRecords.length;i++){
					var aOption = document.createElement("OPTION");
			    	aOption.value = arrDirectorRecords[i][0];
					aOption.text  = arrDirectorRecords[i][1];
					len = lstDirectors.length;
					lstDirectors.options[len]= aOption;
				}
			}else{
				var aOption = document.createElement("OPTION");
			    aOption.value = "-1";
				aOption.text  = "[No entries found]";
				lstDirectors.options[0]= aOption;
			}
		}else{
			//parent.frameMessage.displayMessage("No records found");
			parent.frameMain.displayMessage("No records found");
		}
	}
</script>
</HEAD>
<BODY onload="populateDetails();" bgcolor="#dde22e">
	<form action="merlin" name="frmSearch" method="post">
		<input type="hidden" name="mode">
		<input type="hidden" name="hidSearch">
		<input type="hidden" name="hidCompanies">
		<input type="hidden" name="hidSearchType">
	</form>
</BODY>